package com.hitqz.robot.driver.schedule;

import com.google.common.base.Objects;
import com.hitqz.robot.driver.communication.NetworkServiceThreadPool;
import com.hitqz.robot.driver.util.GfCacheUtil;
import com.hitqz.robot.driver.util.GfToolkit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class GfSchedule {


    private static boolean lastSpeedCtrl = false;

    @Scheduled(fixedRate = 500)
    public void queryStatus(){
        NetworkServiceThreadPool.dealMessage(()->{
            if (GfToolkit.ctx!=null){
                GfToolkit.getRobotStatus();
            }
        });
    }


    /**
     * 挂轨按下去是一直下发速度的，所以没按的时候要下发 0
     */
    @Scheduled(fixedRate = 500)
    private void setRobotStop()  {
        boolean currentSpeedFlag = GfCacheUtil.getSpeedFlag();
        if (!Objects.equal(lastSpeedCtrl, currentSpeedFlag)){
            if (!currentSpeedFlag) {
                // 停止
                GfToolkit.setMotorSpeed((short) 0, (short) 0);
                log.info("[gf]stop move");
            }
        }
        lastSpeedCtrl = currentSpeedFlag;
    }

}
